package com.zwj.liteflow.service.impl;

import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import com.zwj.liteflow.common.LiteFLowParamConstants;
import com.zwj.liteflow.domain.FLowParam;
import com.zwj.liteflow.domain.Result;
import com.zwj.liteflow.domain.params.FlowAParam;
import com.zwj.liteflow.domain.params.LifeParam;
import com.zwj.liteflow.domain.pojo.Body;
import com.zwj.liteflow.domain.result.FlowAResult;
import com.zwj.liteflow.domain.result.LifeResult;
import com.zwj.liteflow.liteflow.context.FlowAContext;
import com.zwj.liteflow.liteflow.context.LifeContext;
import com.zwj.liteflow.service.LiteFlowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 摸鱼二长老
 * @version 1.0
 * @Date 2023/3/2 1:40 PM
 */
@Service
public class LiteFlowServiceImpl implements LiteFlowService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LiteFlowServiceImpl.class);
    private static final String CHAIN_ID = "life2";
    private static final String FLOW_A = "flowA";
    /**
     * 依赖注入liteFlow执行器
     */
    @Resource
    private FlowExecutor flowExecutor;

    @Override
    public Result<LifeResult> myFirstLiteFlowService(LifeParam param) throws Exception {
        Result<LifeResult> result = new Result<>();
        //1.执行器执行
        LiteflowResponse response = flowExecutor.execute2Resp(CHAIN_ID, param, LifeContext.class);

        //2.判断返回结果
        if (!response.isSuccess()) {
            throw response.getCause();
        }
        //3.获取上下文
        LifeContext contextBean = response.getContextBean(LifeContext.class);
        //4.封装返回结果集
        LifeResult lifeResult = new LifeResult();
        lifeResult.setLifeContext(contextBean);
        result.setData(lifeResult);
        return result;
    }

    @Override
    public Result<FlowAResult> myFlowAService(FlowAParam param) throws Exception {
        Result<FlowAResult> result = new Result<>();
        LiteflowResponse response = flowExecutor.execute2Resp(FLOW_A, param, FlowAContext.class);
        if (!response.isSuccess()) {
            throw response.getCause();
        }
        FlowAContext contextBean = response.getContextBean(FlowAContext.class);
        FlowAResult flowAResult = new FlowAResult();
        flowAResult.setFlowAContext(contextBean);
        result.setData(flowAResult);
        return result;
    }

    @Override
    public Result<Body> body(Body param) {
        LiteflowResponse response = flowExecutor.execute2Resp("testBCD", param);
        return null;
    }
}
